package com.zt.weibo.comment;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.zt.weibo.R;
import com.zt.weibo.WeiboApp;
import com.zt.weibo.interfaces.IWeiboPopListener;
import com.zt.weibo.interfaces.IWeiboServiceListener;
import com.zt.weibo.main.WeiboSendPopwin;
import com.zt.weibo.utils.FormatString;
import com.zt.weibo.utils.JsonAnalyze;
import com.zt.weibo.utils.PrintLog;
import com.zt.weibo.utils.SwitchActivity;
import com.zt.weibo.vo.CommentInfo;
import com.zt.weibo.vo.EnumSendType;
import com.zt.weibo.vo.StatusInfo;

public class WeiboCommentActivity extends Activity
{
	private StatusInfo mStatusInfo = null;
	private ArrayList<CommentInfo> mComments = null;
	private ListView mCommentList = null;
	private Button mCommentBtn = null;
	private ServerListener serverListener = null;
	private ProgressDialog mWaitDialog = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		PrintLog.debug("WeiboCommentActivity On Create");
		setContentView(R.layout.weibo_comment);
		WeiboApp.addActivity(this);
		if(null != getIntent().getSerializableExtra("StatusInfo"))
		{
			mStatusInfo = (StatusInfo) getIntent().getSerializableExtra("StatusInfo");
			serverListener = new ServerListener();
			findView();
			initView();
			setListeners();
		}
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		PrintLog.debug("WeiboCommentActivity On Start");
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		PrintLog.debug("WeiboCommentActivity On Resume");
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		PrintLog.debug("WeiboCommentActivity On Pause");
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		PrintLog.debug("WeiboCommentActivity On Stop");
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		PrintLog.debug("WeiboCommentActivity On Destroy");
		WeiboApp.delActivity(this);
	}

	private void findView()
	{
		mCommentList = (ListView) findViewById(R.id.weibo_comment_list);
		mCommentBtn = (Button) findViewById(R.id.weibo_comment_btn);
	}
	
	private void initView()
	{
		mWaitDialog = new ProgressDialog(WeiboCommentActivity.this);
		mWaitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mWaitDialog.setMessage(getString(R.string.progress_dialog_loading));
		WeiboApp.mServer.getComment(mStatusInfo.strId, serverListener);
	}
	
	private void setListeners()
	{
		ClickListener clickListener = new ClickListener();
		ItemClickListener itemClickListener = new ItemClickListener();
		mCommentBtn.setOnClickListener(clickListener);
		mCommentList.setOnItemClickListener(itemClickListener);
	}
	
	private ArrayList<String> getComments(ArrayList<CommentInfo> commentInfos)
	{
		ArrayList<String> comments = new ArrayList<String>();
		for(int i = 0; i < commentInfos.size(); i ++)
		{
			StringBuffer strBuffer = new StringBuffer();
			strBuffer.append(FormatString.formatRemark(commentInfos.get(i).mUserInfo) + "\n");
			strBuffer.append(commentInfos.get(i).strContent);
			comments.add(strBuffer.toString());
		}
		return comments;
	}
	
	private class ClickListener implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			WeiboSendPopwin sendPopwin = new WeiboSendPopwin(WeiboCommentActivity.this, 
					getLayoutInflater().inflate(R.layout.weibo_comment, null), new IWeiboPopListener() {
						
						@Override
						public void onPopShow()
						{
							
						}
						
						@Override
						public void onPopDismiss()
						{
							WeiboApp.mServer.getComment(mStatusInfo.strId, serverListener);
						}
					});
			sendPopwin.setSendType(EnumSendType.E_SEND_COMMENT, mStatusInfo);
			sendPopwin.show();
		}
	}
	
	private class ItemClickListener implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			WeiboCommentMenuPopwin commentMenuPopwin = new WeiboCommentMenuPopwin(WeiboCommentActivity.this,
					getLayoutInflater().inflate(R.layout.weibo_comment, null), new IWeiboPopListener() {
						
						@Override
						public void onPopShow()
						{
							
						}
						
						@Override
						public void onPopDismiss()
						{
							WeiboApp.mServer.getComment(mStatusInfo.strId, serverListener);
						}
					});
			commentMenuPopwin.show(mComments.get(position));
		}
	}
	
	private class ServerListener implements IWeiboServiceListener
	{
		@Override
		public void onStart()
		{
			try
			{
				mWaitDialog.show();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onCompl(String strJson)
		{
			try
			{
				if(mWaitDialog.isShowing())
				{
					mWaitDialog.dismiss();
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			mComments = (ArrayList<CommentInfo>) JsonAnalyze.getInstance().getInfoListFromJson(strJson);
			if(null == mComments)
			{
				return;
			}
			ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(WeiboCommentActivity.this, 
					R.layout.comment_list_item, getComments(mComments));
			mCommentList.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
		}

		@Override
		public void onError(String str)
		{
			StringBuffer strBuffer = new StringBuffer();
			strBuffer.append(str.substring(8, str.length()));
			PrintLog.debug(strBuffer.toString());
			int iErrorCode = Integer.parseInt(strBuffer.toString());
			switch(iErrorCode)
			{
			case 21327://token过期，跳转至登陆界面重新授权
				Toast.makeText(WeiboCommentActivity.this, getString(R.string.toast_token_invaled), 1000).show();
				WeiboApp.mHandler.postDelayed(new Runnable() {
					
					@Override
					public void run()
					{
						SwitchActivity.login(WeiboCommentActivity.this);
						finish();
					}
				}, 1200);
				break;
			}
		}
	}
	
}
